﻿@namespace Gurux.DLMS.AMI.Components.Tab

@inject NavigationManager NavigationManager

<CascadingValue Value="this">
    <ul class="nav nav-tabs"
        role="tablist"
        style="width:100%">
        @foreach (var it in Pages)
        {
            <li class="nav-item" role="presentation">
                <button class="@GetActiveClass(it)"
                        @onclick="@( () => ActivatePage(it))">
                    @if (TabTextTemplate != null)
                    {
                        @TabTextTemplate(it)
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(@it.IconLigature) &&
                        !string.IsNullOrEmpty(@it.Icon))
                        {
                            <span class=@it.Icon aria-hidden="true">
                                @it.IconLigature
                            </span>
                        }
                        else
                        {
                            @if (!string.IsNullOrEmpty(@it.Icon))
                            {
                                <span class=@it.Icon aria-hidden="true"></span>
                            }
                        }
                        @it.Text
                    }
                </button>
            </li>
        }
    </ul>
    @ChildContent
</CascadingValue>

@code {
    /// <summary>
    /// User has selected a new tab.
    /// </summary>
    /// <param name="page">Tab page.</param>
    public delegate void SelectionChangedEventHandler(TabPage page);

    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    [Parameter]
    public int InitialTab { get; set; } = 0;

    [Parameter]
    public string? Active { get; set; }

    /// <summary>
    /// User has selected a new tab.
    /// </summary>
    [Parameter] 
    public SelectionChangedEventHandler? SelectionChanged { get; set; }

    [Parameter]
    public RenderFragment<TabPage>? TabTextTemplate { get; set; }

    public TabPage? ActivePage { get; set; }
    List<TabPage> Pages = new List<TabPage>();
    internal void AddPage(TabPage tabPage)
    {
        tabPage.Index = Pages.Count;
        Pages.Add(tabPage);
        if (!string.IsNullOrEmpty(tabPage.Path) && tabPage.Path == Active)
        {
            ActivePage = tabPage;
        }
        else
        {
            if (InitialTab == 0 && Pages.Count == 1)
            {
                ActivePage = tabPage;
            }
            if (InitialTab != 0 && Pages.Count - 1 == InitialTab)
            {
                ActivePage = Pages[InitialTab];
            }

        }
        StateHasChanged();
    }

    string GetActiveClass(TabPage page)
    {
        return page == ActivePage ? "nav-link active" : "nav-link";
    }

    void ActivatePage(TabPage page)
    {
        ActivePage = page;
        if (!string.IsNullOrEmpty(page.Path))
        {
            NavigationManager.NavigateTo(page.Path);
        }
        SelectionChanged?.Invoke(page);
    }

    public void ActivatePage(int index)
    {
        if (Pages.Count > index)
        {
            ActivePage = Pages[index];
            SelectionChanged?.Invoke(ActivePage);
        }
    }    
}
